<?php
namespace app\entries\model;
use think\Model;

/**
 *
 */
class Pinpai extends Model
{

    //修改品牌信息
    function modifyPpai($id, $data)
    {
        $retval = db('pinpai')->where('id',$id)->update($data);
        // return 1;
        return $retval;
    }

    //添加品牌数据（单个）
    function addpp($data)
    {
        $retval = db('pinpai')->insert($data);
        //关联茶类和产区
        $condition = [
            "teacase_id"=>$data['teacase_id'],
            "teaarea_id"=>$data['teaarea_id'],
        ];
        $res = db('case_area')->where($condition)->find();
        if(!$res){
            db('case_area')->insert($condition);
        }
        // return 1;
        return $retval;
    }

    //删除品牌信息
    function delPp($id)
    {
        $retval = db("pinpai")->where("id", $id)->delete();
        // return 1;
        return $retval;
    }

    //将Excel里的品牌信息导入数据库
    function insertExcelpp($data)
    {
        $teacase_ids = ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            if(!array_key_exists($onedata["teacase"], $teacase_ids)){
                continue;
            }
            if(!array_key_exists($onedata["bangdan"], $bangdan_ids)||$bangdan_ids[$onedata['bangdan']]!=2){
                continue;
            }
            $insertdata =[];

            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$onedata["teaarea"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" =>$onedata['name'],
                "relatedwords" =>$onedata['relatedwords'],
                "sale" =>$onedata['sale'],
                "company" =>$onedata['company'],
                "pnumber" =>$onedata['pnumber'],
                "contact" =>$onedata['contact'],
                "phone" =>$onedata['phone'],
                "wechat" =>$onedata['wechat'],
                "detailintro" =>$onedata['detailintro'],
                "score" => $onedata['score'],
                "addtime" => time()
            ];

            db("pinpai")->insert($insertdata);
        }

        return 1;
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
       $res = db("case_area")->field("teaarea_id")->where("teacase_id",$teacase_id)->select();
       return $res;
    }

    //返回所有品牌信息
    function getAllpp()
    {
        $res = db("pinpai")->order('score desc')->select();
        return $res;
    }

    function getOther()
    {
        $res = db("pinpai")->where('bangdan_id', 4)->order('score desc')->select();
        return $res;
    }







}


?>